package xm.erzhan.filter;

import sun.rmi.rmic.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class CorsConf implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        String header = request.getHeader("Origin");
        response.addHeader("Access-Control-Allow-Origin", header);
        response.addHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE");
        response.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Access-Token");
        response.addHeader("Access-Control-Allow-Credentials","true");
        response.addHeader("Access-Control-Max-Age", "30");
        if (request.getMethod()!= null && "OPTIONS".equals(request.getMethod())) {
            filterChain.doFilter(request, response);
            return;
        }
        filterChain.doFilter(request, response);

    }

    public void destroy() {

    }
}

